home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7629 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: Passing functions as parameters... inconsistent behaviour
  5. Date: 24 Feb 1996 04:23:22 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb23212322@qcd.lanl.gov>
  8. References: <4glbau$tge@inet.up.ac.za>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Rudolph Pienaar's message of 23 Feb 1996 21:24:46 GMT
  13.  
  14. <snip>
  15. RP: void test(float (*passedfunc)()) {
  16. <snip>
  17. RP: float passed(float a){
  18. <snip>
  19. RP:     test( passed );
  20. <snip>
  21. RP: ptr.c: In function `main':
  22. RP: ptr.c:17: warning: passing arg 1 of `test' from incompatible pointer type
  23. RP: 
  24. RP: Of course, this is just a sample... the general rule seems to be that if a
  25. RP: function is passed to another, and if this passed function has any
  26. parameters 
  27. RP: of its own other than *int* (and this includes user defined
  28. types), compilation 
  29. RP: results in a similar warning.
  30.  
  31. You are misjudging the problem: your mistake is that you are mixing
  32. `old' and `new' style declarations. If you had declared test as
  33.    void test(float (*passedfunc)(float))
  34. you would have not seen any problem!
  35.  
  36. Essentially, () in a declaration is an old style declaration. It says
  37. the function in question takes a fixed number of parameters and none
  38. the parameters (if any) are `promotable', i.e. they are not varieties
  39. of char, varieties of short, enums or floats. Do you see the problem
  40. now? 
  41.  
  42. Now why is this? Well, before the `new' style declaration came along,
  43. functions were declared with (), except during definition, when one
  44. used a list of variables without types. A typical definition looked
  45. like 
  46.  
  47. float passed(x)
  48. float x;
  49. {/* body */}
  50.  
  51. Now, though this looks remarkably like 
  52.  
  53. float passed(float x)
  54. {/* body */}
  55.  
  56. there is a very important distinction. Apart from the fact that the
  57. old form of declaration relieves the compiler of any responsibility to
  58. actually check that the number and types of objects passed matched the
  59. declaration, there is another very important difference. The old form
  60. says that when `passed' is called, it is actually passed a double
  61. which is converted _by it_ to a float. (Similarly, char and short are
  62. passed as int instead) The new style declaration on the other hand
  63. says that the compiler can pass the float as a float. Thus, contrary
  64. to appearances, the `old' form is not compatible with `float
  65. passed(float)', but rather with `float passed(double)'. In fact, it is
  66. not posible to write an old style declaration compatible with `float
  67. passed(float)'. 
  68.  
  69. Because of this historical state of affairs, `float passed()'
  70. _is_ compatible with `float passed(double)', but not with `float
  71. passed(float)'. 
  72.  
  73. Cheers
  74. Tanmoy
  75. --
  76. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  77. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  78. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  79. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  80. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  81. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  82.